package com.sure.yyr.security.user;

import com.sure.yyr.common.Result;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController //说明这是一个受spring容器管理的控制器组件(Bean)
@RequestMapping("/security/user") //本控制器的路径前缀
public class UserAPI {


    //引入依赖对象
    @Resource //此注解表示从容器中引入依赖对象
    private UserService userService;


    /**
     * 查询用户清单
     * @param dto
     * @return
     */
    @GetMapping("")
    public Result queryUsers(UserDto dto){

        Map<String,Object> page =  userService.getUserPage(dto);
        return Result.success(page);
    }

    @GetMapping("/{u_id}")
    public Result queryUserById(@PathVariable String u_id){
        User user =  userService.getUserById(u_id);
        return Result.success(user);
    }


    /**
     * 修改用户信息
     * @param dto
     * @return
     */
    @PostMapping("")
    public Result addUser(@RequestBody UserDto dto){
        userService.addUser(dto);
        return Result.success();
    }

    /**
     * 修改用户信息
     * @param dto
     * @return
     */
    @PutMapping("")
    public Result updateUser(@RequestBody UserDto dto){
        userService.updateUser(dto);
        return Result.success();
    }

    /**
     * 按照账号删除一个用户信息
     * @param u_id
     * @return
     */
    @DeleteMapping("/{u_id}")
    public Result deleteUser(@PathVariable String u_id){
        userService.deleteUser(u_id);
        return Result.success();
    }

    /**
     * 根据多个账号删除多个用户信息
     * @param u_ids
     * @return
     */
    @DeleteMapping("")
    public Result deleteUser(@RequestBody String[] u_ids){
        userService.deleteUser(u_ids);
        return Result.success();
    }

    /**
     * 启用用户
     * @param u_id
     * @return
     */
    @PutMapping("/enable/{u_id}")
    public Result enableUser(@PathVariable String u_id){
       userService.enableUser(u_id);
       return Result.success();
    }

    /**
     * 禁用用户
     * @param u_id
     * @return
     */
    @PutMapping("/disable/{u_id}")
    public Result disableUser(@PathVariable String u_id){
        userService.disableUser(u_id);
        return Result.success();
    }
    @GetMapping("/role/{u_id}")
    public Result getRoles(@PathVariable String u_id){
        List<Role> roleList = userService.getRoles(u_id);
        return Result.success(roleList);
    }
    @PutMapping("/role/{u_id}")
    public Result setRoles(@PathVariable String u_id,@RequestBody Integer[] ro_ids){
        userService.setRoles(u_id,ro_ids);
        return Result.success();
    }



}
